<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['allocation:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['allocation:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" :index="createIndex" fixed="left"/>
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="电话" align="center" prop="telPhone" />
      <el-table-column label="办公电话" align="center" prop="phone" />
      <el-table-column label="会内职务" align="center" prop="internalJob" />
      <el-table-column label="社会职务" align="center" prop="societyJob" :show-overflow-tooltip="true">
         <template slot-scope="scope">
          <span>{{scope.row.societyJob}}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮箱地址" align="center" prop="email" />
      <el-table-column label="头像地址" align="center" prop="headUrl">
          <!-- 图片的显示 -->
                 <template   slot-scope="scope">            
                    <img :src="scope.row.headUrl"  min-width="70" height="70" 
                           @click="previewPic(scope.row.headUrl)" />
                 </template>     
   
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="关注时间" align="center" prop="createTime" width="180">
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" 
       :show-overflow-tooltip="true">
         <template slot-scope="scope">
          <span>{{scope.row.address}}</span>
        </template>
      </el-table-column>
      <el-table-column label="职位名称" align="center" prop="jobName" />
      <el-table-column label="用户类型" align="center" prop="userType" > 
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_type" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="会员类型" align="center" prop="vipType" >
       <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_platform_type" :value="scope.row.vipType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['allocation:user:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  <!-- 图片预览   放在表格外面，这是个弹出框-->
      <el-dialog :visible.sync="dialogVisible" :modal="false" title="图片预览" width="50%">
        <img :src="previewpic" alt=""  width="100%" />
        <span slot="footer" class="dialog-footer">
          </span>
       </el-dialog>
  <!-- 图片预览 -->
    <!-- 添加或修改微信用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
         <el-form-item label="会员类型" prop="nickName">
           <el-select v-model="form.vipType" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
           <el-form-item label="会员所属分组" prop="nickName">
          <el-select v-model="form.articleType" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="团体名称" prop="nickName">
          <el-input v-model="form.firmName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="会内职务" prop="internalJob">
          <el-input v-model="form.internalJob" placeholder="请输入会内职务" />
        </el-form-item>
        <el-form-item label="社会职务" prop="societyJob">
          <el-input v-model="form.societyJob" placeholder="请输入社会职务" />
        </el-form-item>
        <el-form-item label="电话" prop="telPhone">
          <el-input v-model="form.telPhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="办公电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入办公电话" />
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱地址" />
        </el-form-item>
        <el-form-item label="新老会员" prop="isOld">
          <el-radio v-model="form.isOld" label="1">是</el-radio>
          <el-radio v-model="form.isOld" label="2">否</el-radio>
        </el-form-item>
        <el-form-item label="性别" prop="email">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="头像地址" prop="headUrl">
          <el-upload
          class="avatar-uploader"
          ref="upload"
          :action="upload.url"
          :headers="upload.headers"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-form-item>
        <el-form-item label="地址" prop="detailedAddress">
          <el-input v-model="form.detailedAddress"/>
          <div style="width:100%;border: 2px solid #ced4da;padding: 2px;margin: 10px 0">
              <iframe id="mapPage" style="width: 100%;height: 600px;z-index: 999" frameborder=0
                    src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=5OVBZ-27I6W-5LNRB-OWUIN-FUCVF-5SF6P&referer=云社群-H5">
              </iframe>
              </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/allocation/user";
import { getToken } from "@/utils/auth";
export default {
  name: "User",
   dicts: ['sys_member_audit_type','sys_user_type','sys_platform_type','sys_user_sex'],
  data() {
    return {
     options:[],
     dialogVisible: false,
        previewpic: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
       //上传参数
      upload:{
          // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/content/advertising/importData"
      },
    
      // 是否显示弹出层
      open: false,
      imageUrl:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        realName: null,
        telPhone: null,
        phone: null,
        internalJob: null,
        societyJob: null,
        email: null,
        headUrl: null,
        openId: null,
        sex: null,
        birthday: null,
        address: null,
        profession: null,
        firmName: null,
        jobName: null,
        asId: null,
        userType: null,
        vipType: null,
        isOld: null,
        groupNameId: null,
        finishData: null,
        status: null,
        authOpenId: null,
        unionId: null,
        contactsNo: null,
        detailedAddress: null,
        type: null,
        articleType: null,
         
      },
      mainVm:{},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        asId: [
          { required: true, message: "协会id不能为空", trigger: "blur" }
        ],
      }
    };
  },
mounted(){
      //接收来自其他页面的消息并判断发送者
    window.addEventListener("message", this.onMessage);

},
  created() {
    this.getList();
  },
  methods: {
    createIndex(index){
      return (this.queryParams.pageNum - 1) *this.queryParams.pageSize + index + 1;
    },
    //获取地址
    onMessage(event) {
	  // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
	  var loc = event.data;
	  if (loc && loc.module == 'locationPicker') {
      //防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
	      console.log('location', loc);
        if(loc.poiname=='我的位置'){
          this.$modal.confirm("请重新选择地址")
        }
        this.form.detailedAddress=loc.cityname+"·"+loc.poiname;
        let obj={};
        obj.name=loc.poiname;
        obj.coord=loc.latlng.lat+","+loc.latlng.lng;
        this.form.address=JSON.stringify(obj);
        console.info(this.form.address);
	      this.mainVm.dialogFormVisibleMap=false;
	  }
},
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(res => {
        console.info(res);
        this.userList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    previewPic(url) {
           this.previewpic = url;
           this.dialogVisible = true;
        },
         //上传图片地址
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        if(res.code==200){
          console.info(res.data);
          this.form.pcUrl=res.data;
        }else{
          console.info(res.msg)
        }
      },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nickName: null,
        realName: null,
        telPhone: null,
        phone: null,
        internalJob: null,
        societyJob: null,
        email: null,
        headUrl: null,
        openId: null,
        sex: null,
        birthday: null,
        address: null,
        profession: null,
        firmName: null,
        jobName: null,
        asId: null,
        userType: null,
        vipType: null,
        isOld: null,
        groupNameId: null,
        finishData: null,
        createTime: null,
        updateTime: null,
        status: 0,
        authOpenId: null,
        unionId: null,
        contactsNo: null,
        detailedAddress: null,
        type: null,
        articleType: null,
        addres:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.imageUrl=response.data.headUrl;
        this.open = true;
        this.title = "修改微信用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微信用户编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('allocation/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style >

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
.avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
